package com.briup.estore.web.controller;

import com.briup.estore.Service.BookService;
import com.briup.estore.vo.BookInfoVO;
import com.briup.estore.vo.BookVO;
import com.briup.estore.web.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController  //类的对象放入容器
@Api(tags = "图书模块") //类的含义
@RequestMapping("/book")

public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("/showBooks")
    @ApiOperation("首页展示图书信息 只展示20本")
    //暴露方法
    public Result showBooks() {
        List<BookVO> books = bookService.showBooks();
        return Result.success(books);
    }


    @GetMapping("/findByCategory/{categoryId}")
    @ApiOperation("目录查书")
    public Result findByCategory(@PathVariable int categoryId){
        List<BookVO> books = bookService.findByCategory(categoryId);
        return  Result.success(books);
    }

    @GetMapping("/searchBook")
    @ApiOperation("图书信息检索根据书名、作者、出版社")
    public Result searchBooks( String query){
        List<BookVO> books = bookService.getBook(query);
        return Result.success(books);
    }



    @GetMapping ("/{id}")//    /book/12
    @ApiOperation("图书详情")
    public Result getBook(@PathVariable Integer id, HttpServletRequest request){
        //取出用户id
        String userId = (String) request.getAttribute("userId");
        //调用service 查看书籍详情  book category collect
        BookInfoVO book = bookService.getBookById(id,Integer.parseInt(userId));
        return Result.success(book);
    }
}
